<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Image Upload</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f4f4f4;
            font-family: Arial, sans-serif;
        }
        .container {
            text-align: center;
            padding: 30px;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        input[type="file"] {
            margin: 20px 0;
        }
        button {
            padding: 10px 20px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        button:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>Upload an Image</h2>
        <input type="file" id="imageInput" accept="image/*">
        <button onclick="uploadImage()">Upload</button>
        <p id="result"></p>
    </div>

    <script>
        function uploadImage() {
            const fileInput = document.getElementById('imageInput');
            const file = fileInput.files[0];
            if (!file) {
                alert('Please select an image!');
                return;
            }

            const formData = new FormData();
            formData.append('image', file);

            fetch('http://127.0.0.1:5000/upload', {  // 替换 <your-server-ip> 为服务器 IP
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                if (data.image_id) {
                    document.getElementById('result').textContent =
                        `Upload successful! Image ID: ${data.image_id}`;
                } else {
                    alert('Error: ' + data.error);
                }
            })
            .catch(error => console.error('Error:', error));
        }
    </script>
</body>
</html>
